package com.jqweb.services;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jqweb.controller.Article.pojo.ArticleByCategoryDto;
import com.jqweb.dao.ArticleDao;
import com.jqweb.entity.ArticleEntity;
import com.jqweb.utils.Paging;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class ArticleService {
    @Autowired
    private ArticleDao articleDao;

    // 分页获取文章列表
    public Map<String, Object> getAll(Paging paging) {
        // 分页查询
        IPage<ArticleEntity> page = new Page<>(paging.getPage(), paging.getLimit());
        QueryWrapper<ArticleEntity> queryWrapper = new QueryWrapper<>();
        IPage<ArticleEntity> result = articleDao.selectPage(page,queryWrapper);
        Map<String, Object> response = new HashMap<>();
        response.put("list", result.getRecords());
        response.put("total", result.getTotal());
        return response;
    }

    // 根据id获取文章详情
    public ArticleEntity getById(Integer id) {
        return articleDao.selectById(id);
    }

    // 根据分类获取文章列表
    public Map<String, Object> getByCategory(ArticleByCategoryDto articleByCategoryDto) {
        // 分页查询
        IPage<ArticleEntity> page = new Page<>(articleByCategoryDto.getPage(), articleByCategoryDto.getLimit());
        QueryWrapper<ArticleEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category", articleByCategoryDto.getCategory());
        IPage<ArticleEntity> result = articleDao.selectPage(page,queryWrapper);
        Map<String, Object> response = new HashMap<>();
        response.put("list", result.getRecords());
        response.put("total", result.getTotal());
        return response;
    }

    // 创建文章
    public int createArticle(ArticleEntity articleEntity) {
        ArticleEntity articleEntity1 = new ArticleEntity();
        articleEntity1.setTitle(articleEntity.getTitle());
        articleEntity1.setContent(articleEntity.getContent());
        articleEntity1.setManager_id(articleEntity.getManager_id());
        articleEntity1.setManager_name(articleEntity.getManager_name());
        articleEntity1.setCategory(articleEntity.getCategory());
        articleEntity1.setColor(articleEntity.getColor());
        articleEntity1.setTags(articleEntity.getTags());
        return articleDao.insert(articleEntity);
    }

    // 修改文章
    public Integer updateArticle(ArticleEntity articleEntity) {
        ArticleEntity articleEntity1 = new ArticleEntity();
        articleEntity1.setId(articleEntity.getId());
        articleEntity1.setTitle(articleEntity.getTitle());
        articleEntity1.setContent(articleEntity.getContent());
        articleEntity1.setManager_id(articleEntity.getManager_id());
        articleEntity1.setManager_name(articleEntity.getManager_name());
        articleEntity1.setCategory(articleEntity.getCategory());
        articleEntity1.setColor(articleEntity.getColor());
        articleEntity1.setTags(articleEntity.getTags());

        return articleDao.updateById(articleEntity);
    }

    // 删除文章
    public Integer deleteArticle(Integer id) {
        return articleDao.deleteById(id);
    }
}
